package com.wibo.account;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class ImageSizeSettingsPreference extends DialogPreference {
	private int mImageSize = 50;
	private Context mContext;
	
	private TextView mTv;
	private SeekBar mSeekBar;
	
	protected void onBindDialogView(View view) {   
        super.onBindDialogView(view);   
        SharedPreferences settings = mContext.getSharedPreferences(
				WiboMainActivity.USER_INFO, 0);
		mImageSize = settings.getInt(WiboMainActivity.IMAGE_QUALITY, 50);
		
        mTv = (TextView) view.findViewById(R.id.tv);
        mSeekBar = (SeekBar) view.findViewById(R.id.seekBar);
        mTv.setText(mContext.getString(R.string.imageSize2) + String.valueOf(mImageSize)
				+ "%");
        
        mSeekBar.setProgress(mImageSize);

        mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				mImageSize = progress;
				mTv.setText(mContext.getString(R.string.imageSize2)
						+ String.valueOf(mImageSize) + "%");

				
			}
		});
	}
	public ImageSizeSettingsPreference(Context context, AttributeSet attrs) {   
        super(context, attrs);         
        setDialogLayoutResource(R.layout.image_preference);  //加载布局文件   
        
        mContext = context;
        setTitle(R.string.imageSize);

    }
	
//	protected void onDialogClosed(boolean positiveResult) {   
//        super.onDialogClosed(positiveResult);  
//       
//	}
	
	/* (non-Javadoc)
	 * @see android.preference.DialogPreference#onClick(android.content.DialogInterface, int)
	 */
	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		switch (which) {
		case Dialog.BUTTON_POSITIVE:
			 SharedPreferences settings = mContext.getSharedPreferences(
						WiboMainActivity.USER_INFO, 0);
				Editor editor = settings.edit();
				editor.putInt(WiboMainActivity.IMAGE_QUALITY, mImageSize);
				editor.commit();
			break;
		case Dialog.BUTTON_NEGATIVE:
			break;
		}
		super.onClick(dialog, which);
	}
	@Override
	protected Object onGetDefaultValue(TypedArray a, int index) {
		return a.getString(index);
	}

	
}
